<?
	require_once "_MemberFunc.php";
	
	function &GetJBNHOptionList($jbnh_seq)
	{
		$str = "";
		$db = GetDefaultConnection();
		$sql =       "select "
						." jbnh_seq, "
						." jbnh_name "
						." from TB_JBNH "
						." where del_yn='N' ";
		$list = $db->queryList($sql);
		
		$str .="<select name='jbnh_seq' id='jbnh_seq' >";
		if ($list->getLength() > 0)
		{
			$str .="<option value=''>소속농협선택</option>";
			while ($list->hasNext())
			{
				$row = & $list->next();
				$selStr = "";
				if ( $jbnh_seq == $row->getNumber('jbnh_seq') )
					$selStr = "selected";
				$str .="<option value='". $row->getNumber('jbnh_seq') . "' $selStr >" . $row->getString('jbnh_name') . "</option>";
			}
		}
		$str .="</select>";
		return $str;		
	}
	
	function &GetMngJBNHCheckboxList($arr_jbnh_seq, $getAll = false)
	{
		$db = GetDefaultConnection();
		$sql =       "select "
						." jbnh_seq, "
						." jbnh_name "
						." from TB_JBNH "
						." where del_yn='N' ";						
		$list = $db->queryList($sql);
		
		$str = "";
		$selstr = "";
		
		if ($list->getLength() > 0)
		{
			while ($list->hasNext())
			{
				$row = & $list->next();
				$selstr = "";
				if (is_array($arr_jbnh_seq)){
					foreach ($arr_jbnh_seq as $val)
					{
						if ( $val == $row->getString('jbnh_seq'))
							$selstr = " checked ";
					}
				}				
              	$str .= "<li><input type='checkbox' id='mng_jbnh_seq[]' name='mng_jbnh_seq[]' value='". $row->getString('jbnh_seq') ."' $selstr />". $row->getString('jbnh_name') ."</li>";
			}
		}
		return $str;		
	}
	
	function &GetMngJBNHArrayList($member_id)
	{
		$rtn_array = array();
		$db = GetDefaultConnection();
		$sql = sprintf(
						"select "
						." jbnh_seq "
						." from TB_MANAGER "
						." where member_id = '%s' "
					,$member_id
					);
		$list = $db->queryList($sql);
		
		if ($list->getLength() > 0)
		{
			while ($list->hasNext())
			{
				$row = & $list->next();
				array_push($rtn_array, $row->getString('jbnh_seq') );
			}
		}
		return $rtn_array;
	}
	
	function &GetMemberList()
	{
		$db = GetDefaultConnection();
		$sql = sprintf(
						"select "
						." member_seq, "
						." member_id, "
						." member_grade, "
						." (select jbnh_name from TB_JBNH where t.jbnh_seq = jbnh_seq) as jbnh_name, "				
						." member_delyn "
						." from TB_MEMBER t "
						." where 1 = '%s' "
					,"1"
					);
		return $db->queryList($sql);
	} 
	
	function &GetMemberExcelList($getHeader=false)
	{
		$db = GetDefaultConnection();
		
		$query =  		"select "
						."	member_seq as '번호', "
						. RtnExcelText(member_id, "아이디")
						."	CASE member_grade "
						."		WHEN '1' THEN '전체관리자' "
						."		WHEN '2' THEN '지역농협관리자' "
						."		WHEN '3' THEN '지역관리자' "
						."	END as '권한'	, "
						."	(select jbnh_name from TB_JBNH where t.jbnh_seq = jbnh_seq) as '관리농협', " 				
						. RtnExcelText(member_pwd, "암호")
						. RtnExcelText(member_hp, "핸드폰")
						."	member_update	as '수정시각', "
						."	member_regdate	as '등록시각', "
						."	CASE member_delyn "
						."		WHEN 'Y' THEN '아니오' "
						."		WHEN 'N' THEN '예' "
						."	END as '사용여부' "
						."	from TB_MEMBER t "	
						." where 1 = '%s' ";
		if ($getHeader) {
			$query .= " limit 0,1 ";
		}									
		
		$sql = sprintf( $query,"1");
		return $db->queryList($sql);
	} 

	function &GetFarmExcelList($getHeader=false, $sch_farm_name = null)
	{
		$db = GetDefaultConnection();
		$query = "select "
				." farm_seq				as '고유값', "
				." jbnh_seq				as '농협코드', "
				." (select jbnh_name from TB_JBNH where t.jbnh_seq = jbnh_seq) 	as '농협명', "
				." farm_name			as '생산자', "
				." farm_address			as '주소', "
				." farm_tel				as '연락처', "
				." farm_itemcode1		as '품목코드', "
				." (select code_name from TB_CODE where code_type='A' and code_code1=t.farm_itemcode1 and code_code2=0) 	as '품목명' , "
				." farm_regnumber		as '출하자신고번호', "
				." farm_certnumber		as '품질인증번호', "
				." farm_gapnumber		as 'GAP인증번호', "
				." farm_tracenumber		as '이력추적관리번호', "
				." farm_certdate		as '인증만료기간', "
				." farm_memo			as '비고사항', "
				." farm_regdate	 		as '등록일'"
				." from TB_FARM t ";
				
		$keyword = "1";
		
		if ( $sch_farm_name != null ) {				
			$query .= " where farm_name like '%s' ";
			$keyword = "%".$sch_farm_name."%";
		}
		else {
			$query .= " where 1 = '%s'  ";
		}
		
		$query .= " and del_yn='N' ";
		
		if ($getHeader) {
			$query .= " limit 0,1 ";
		}	
		
		$sql = sprintf( $query, $keyword );
		
		return $db->queryList($sql);
	} 
	
	function RtnMemberGrade($grade)
	{
		$gradeName = "";
		if ( $grade == 1 )
			$gradeName = "전체관리자";
		else if ( $grade == 2 )
			$gradeName = "지역농협관리자";
		else if ( $grade == 3 )
			$gradeName = "지역관리자";
		return $gradeName;
	}
	
	function &GetMemberInfo($member_seq)
	{
		return Member_GetMemberInfo(null,$member_seq, false);
	}
	
	function MemberIdExist ($member_id) {
		$member_id = noSqlInjection($member_id);
		$db = GetDefaultConnection();
		$sql = sprintf(
						"select "
						." count(member_id) as cnt "
						." from TB_MEMBER "
						." where member_id= '%s' "
					,$member_id
					);
		$row =& $db->queryRow($sql);
		return ( $row->getNumber("cnt") > 0 );	
	}

	function MemberInsert($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_MEMBER";

		$now = date('Y-m-d H:i:s');
		$array['member_regdate']	= $now;
	
		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}
	
	function MemberUpdate($array, $member_seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_MEMBER";

		$now = date('Y-m-d H:i:s');
		$array['member_update']	= $now;
		
		return $db->queryUpdate($table_name, $array, " (member_seq = '$member_seq')");
	}
	
	function ManagerInsert($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_MANAGER";

		$now = date('Y-m-d H:i:s');
		$array['manager_regdate']	= $now;
	
		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}	
	
	function ManagerDelete($member_id)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_MANAGER";
		$result = $db->queryDelete($table_name, " (member_id = '$member_id')");
		return $result;
	}	
	
	function MemberDelete($member_seq, $rowDel = false)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_MEMBER";
		if ( $rowDel ) {
			$member_seq = noSqlInjection($member_seq);
			$result = $db->queryDelete($table_name, " (member_seq = '$member_seq')");
		} 
		else
		{
			$array['member_delyn']	= "Y";
			$result = $db->queryUpdate($table_name, $array, " (member_seq = '$member_seq') ");
		}
		return $result;
	}	
	
	function &GetFarmList($sch_farm_name = null, $page, $pagesize)
	{
		$db = GetDefaultConnection();

		$query = "select "
				." farm_seq	, "
				." jbnh_seq	, "
				." (select jbnh_name from TB_JBNH where t.jbnh_seq = jbnh_seq) as jbnh_name, "
				." farm_name	, "
				." farm_address	, "
				." farm_tel	, "
				." farm_itemcode1	, "
				." (select code_name from TB_CODE where code_type='A' and code_code1=t.farm_itemcode1 and code_code2=0) as farm_itemname1 , "
				." farm_regnumber	, "
				." farm_certnumber	, "
				." farm_gapnumber	, "
				." farm_tracenumber	, "
				." farm_certdate	, "
				." farm_memo	, "
				." farm_regdate	 "
				." from TB_FARM t ";
				
		$keyword = "1";
		
		if ( $sch_farm_name != null ) {				
			$query .= " where farm_name like '%s' ";
			$keyword = "%".$sch_farm_name."%";
		}
		else {
			$query .= " where 1 = '%s'  ";
		}
		
		$query .= " and del_yn='N' ";
		

		$start = ($page - 1) * $pagesize;
		$limit = " LIMIT $start, $pagesize";
		$query = $query . $limit;

		
		$sql = sprintf( $query, $keyword );
		
		return $db->queryList($sql);
	} 
	
	function &GetFarmListCount($sch_farm_name = null)
	{
		$db = GetDefaultConnection();

		$query = "select "
				." count(jbnh_seq) as cnt "
				." from TB_FARM t ";
				
		$keyword = "1";
		
		if ( $sch_farm_name != null ) {				
			$query .= " where farm_name like '%s' ";
			$keyword = "%".$sch_farm_name."%";
		}
		else {
			$query .= " where 1 = '%s'  ";
		}
		
		$query .= " and del_yn='N' ";
		
		$sql = sprintf( $query, $keyword );
		$row = $db->queryRow($sql);
		return $row->getNumber("cnt");
	} 	
	
	function FarmInsert($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_FARM";

		$now = date('Y-m-d H:i:s');
		$array['farm_regdate']	= $now;
	
		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}
	
	function FarmUpdate($array, $farm_seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_FARM";
		return $db->queryUpdate($table_name, $array, " (farm_seq = '$farm_seq')");
	}
	
	function FarmDelete($farm_seq)
	{
		
		$result = FarmItemDelete($farm_seq);
		if ( $result ) {
			$db = GetDefaultConnection();
			$table_name = "TB_FARM";
			//$result = $db->queryDelete($table_name, " (farm_seq = '$farm_seq')");
			$array = array( 'del_yn' => "Y",);
			$result = $db->queryUpdate($table_name, $array, " (farm_seq = '$farm_seq')");			
			
			
		}
		return $result;		
	}
	
	
	function FarmItemInsert($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_FARMITEM";

		$now = date('Y-m-d H:i:s');
		$array['farmitem_regdate']	= $now;
	
		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}

	function FarmItemDelete($farm_seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_FARMITEM";
		$result = $db->queryDelete($table_name, " (farm_seq = '$farm_seq')");
		return $result;
	}
	
	function &GetFarmItem2List($farm_seq, $farm_itemcode1)
	{
		$db = GetDefaultConnection();
		$sql = sprintf(
						"select "
						." farmitem_seq	, "
						." farm_seq	, "
						." farmitem_item2, "
						." (select code_name from TB_CODE where code_type='A' and code_code1='%s' and code_code2=t.farmitem_item2) as farm_itemname2 "
						." from TB_FARMITEM t "
						." where farm_seq = '%s' "
						." order by farmitem_item2 "
					,$farm_itemcode1
					,$farm_seq
					);
		return $db->queryList($sql);
	}
	
	function &GetFarmItem2ListHTML($farm_seq, $farm_itemcode1, $getCode = false)
	{
		$rtnHTML = "";
		$list = GetFarmItem2List($farm_seq, $farm_itemcode1);
		if ($list->getLength() > 0) {
			while ($list->hasNext())
			{
				$row 			= & $list->next();
				if ( $getCode )
					$farm_item2 = $row->getString('farmitem_item2');
				else 
					$farm_item2 = $row->getString('farm_itemname2');
				if ( $rtnHTML != "" )
					$rtnHTML.= ",";
				$rtnHTML .= $farm_item2;
			}
		}
		return $rtnHTML;
	}	

	function &GetFarmInfo($farm_seq )
	{
		$db =& GetDefaultConnection();
		$query = "SELECT "
				." farm_seq	, "
				." jbnh_seq	, "
				." (select jbnh_name from TB_JBNH where t.jbnh_seq = jbnh_seq) as jbnh_name, "
				." farm_name	, "
				." farm_address	, "
				." farm_tel	, "
				." farm_itemcode1	, "
				." (select code_name from TB_CODE where code_type='A' and code_code1=t.farm_itemcode1 and code_code2=0) as farm_itemname1 , "
				." farm_regnumber	, "
				." farm_certnumber	, "
				." farm_gapnumber	, "
				." farm_tracenumber	, "
				." farm_certdate	, "
				." farm_memo	, "
				." farm_regdate	 "
				." from TB_FARM t "
				." where farm_seq = '%s' "
				." and del_yn='N' ";
		$sql = sprintf($query,$farm_seq);
		$row =& $db->queryRow($sql);
		return $row;
	}
	
	
	
	function &GetNHList()
	{
		$db = GetDefaultConnection();
		$sql = sprintf(
						"select "
						." jbnh_seq	, "
						." jbnh_name "
						." from TB_JBNH t "
						." where del_yn = '%s' "
					,"N"
					);
		return $db->queryList($sql);
	}
		
	function NHInsert($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_JBNH";

		$now = date('Y-m-d H:i:s');
		$array['jbnh_regdate']	= $now;
	
		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}
	
	function NHCodeInsert($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_NHCODE";

		$now = date('Y-m-d H:i:s');
		$array['nhcode_regdate']	= $now;
	
		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}

	function NHDelete($jbnh_seq)
	{
		$result = NHCodeDelete($jbnh_seq);
		if ( $result ) {
			$db = GetDefaultConnection();
			$table_name = "TB_JBNH";
			//$result = $db->queryDelete($table_name, " (jbnh_seq = '$jbnh_seq')");
			$array = array( 'del_yn' => "Y",);
			$result = $db->queryUpdate($table_name, $array, " (jbnh_seq = '$jbnh_seq')");
		}
		return $result;		
	}	
	
	function NHUpdate($array, $jbnh_seq)	
	{
		$db = GetDefaultConnection();
		$table_name = "TB_JBNH";
		return $db->queryUpdate($table_name, $array, " (jbnh_seq = '$jbnh_seq')");
	}	
	
	function NHCodeDelete($jbnh_seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_NHCODE";
		//$result = $db->queryDelete($table_name, " (jbnh_seq = '$jbnh_seq')");
		$array = array( 'del_yn' => "Y",);
		$result = $db->queryUpdate($table_name, $array, " (jbnh_seq = '$jbnh_seq')");
		
		return $result;
	}
	function & GetNHInfo($jbnh_seq)
	{
		$db =& GetDefaultConnection();
		$query = "SELECT "
				." jbnh_seq	, "
				." jbnh_name  "
				." from TB_JBNH t "
				." where jbnh_seq = '%s' "
				." and del_yn = 'N' ";
		$sql = sprintf($query,$jbnh_seq);
		$row =& $db->queryRow($sql);
		return $row;
	}
	
	function &GetNHCodeList($jbnh_seq)
	{
		$db = GetDefaultConnection();
		$sql = sprintf(
						"select "
						." nhcode_seq, "
						." jbnh_seq, "
						." nhcode_item1 "
						." from TB_NHCODE t "
						." where jbnh_seq = '%s' "
						." and del_yn='N' "						
					,$jbnh_seq
					);
		return $db->queryList($sql);
	}
	
	function &GetNHCodeListHTML($jbnh_seq)
	{
		$rtnHTML = "";
		$list = GetNHCodeList($jbnh_seq);
		if ($list->getLength() > 0) {
			while ($list->hasNext())
			{
				$row 			= & $list->next();
				$nhcode_item1 = $row->getString('nhcode_item1');
				if ( $rtnHTML != "" )
					$rtnHTML.= ",";
				$rtnHTML .= $nhcode_item1;
			}
		}
		return $rtnHTML;
	}
	
	function CodeDelete($code_seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_CODE";
		
		//$result = $db->queryDelete($table_name, " (code_seq = '$code_seq')");
		$array = array( 'del_yn' => "Y",);
		$result = $db->queryUpdate($table_name, $array, " (code_seq = '$code_seq')");
		
		return $result;
	}	
	
	function CodeInsert($array)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_CODE";

		$now = date('Y-m-d H:i:s');
		$array['code_regdate']	= $now;
	
		$result = $db->queryInsert($table_name, $array);
		$no = mysql_insert_id();

		if (!$result)
			return -1;
		else
			return $no;
	}
	
	function GetCodeNumForInsert($type, $level, $code)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_CODE";
		$sql = "SELECT COUNT(code_seq) AS cnt FROM $table_name ";
		$sql .= " WHERE code_type = '$type' ";
		if ( $level == '1' )
			$sql .= " AND code_code2 = 0 ";
		else 
			$sql .= " AND code_code1='$code' AND code_code2 > 0 ";
			
		$row =& $db->queryRow($sql);
		$cnt = $row->getNumber('cnt');
		return $cnt+1; 
	}	
	
	function CodeUpdate($array, $code_seq)
	{
		$db = GetDefaultConnection();
		$table_name = "TB_CODE";
		return $db->queryUpdate($table_name, $array, " (code_seq = '$code_seq')");
	}
	
	function GetCodeInfo($code_seq){
		$db =& GetDefaultConnection();
		$query = "SELECT "
				." code_seq	, "
				." code_name	, "
				." code_type	, "
				." code_code1	, "
				." code_code2	, "
				." code_display  "
				." from TB_CODE t "
				." where code_seq = '%s' ";
		$sql = sprintf($query,$code_seq);
		$row =& $db->queryRow($sql);
		return $row;
	}	
	
	function printPageList($page, $totalpage, $pagelistsize, $urlbase, $param = null)
	{
		$totalpagelist = ceil($totalpage/$pagelistsize);
		$pagelist = ceil($page/$pagelistsize);
		if ($pagelist > $totalpagelist)
			$pagelist = $totalpagelist;
		if ($pagelist < 1)
			$pagelist = 1;
	
		$startpagelist = ($pagelist - 1) * $pagelistsize + 1;
		$endpagelist = $startpagelist + $pagelistsize - 1;
		if ($endpagelist > $totalpage)
			$endpagelist = $totalpage;
		if ($endpagelist < 1)
			$endpagelist = 1;
		unset($param['page']);
		$parambase = encodeParamArray($param);
	
		if (strstr($urlbase, '?'))
			$urlbase .= '&';
		else
			$urlbase .= '?';
	
		if ($parambase)
			$urlbase .= $parambase . "&";

	
		echo "<a href='" . $urlbase . "page=1' class='direction prev'><span></span><span></span> 처음</a>";
		if ($startpagelist > 1)
		{
			echo "<a href='" . $urlbase . "page=" . ($startpagelist-1) . "' class='direction prev'><span></span> 이전</a>";
		}
		else
		{
			echo "<a href='" . $urlbase . "page=1' class='direction prev'><span></span> 이전</a>";
		}
	
		for ($i = $startpagelist; $i <= $endpagelist; $i++)
		{
			if ($i == $page)
				echo "<strong>$i</strong>";
			else
				echo "<a href='" . $urlbase . "page=" . $i . "'>" . $i . "</a>";
		}
	
		if ($endpagelist < $totalpage)
		{
			echo "<a href='" . $urlbase . "page=" . ($endpagelist + 1) . "' class='direction next'>다음 <span></span></a>";
		}
		else
		{
			echo "<a href='" . $urlbase . "page=" . $totalpage . "' class='direction next'>다음 <span></span></a>";
		}
		echo "<a href='" . $urlbase . "page=" . $totalpage . "' class='direction next'>끝 <span></span><span></span></a>";
	}	
	
	//login check
	require_once "_CheckLogin.php";
?>	